import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Picturegrid extends JPanel {
	public Image image;
	public static ArrayList<String> myArr = new ArrayList<String>();
	public static ArrayList<String> myArr1 = new ArrayList<String>();
	
	public Picturegrid(ArrayList<String> piclist){
		setLayout(new GridLayout(piclist.size()/4+1,4,10,10));
			for (int i=0;i<piclist.size();i++)
		{
			URL where;
			try {
				where = new URL(piclist.get(i));
				ImageIcon icon = new ImageIcon(where);
				ImageIcon thumbnail=new ImageIcon(getScaledImage(icon.getImage(), 80, 80)); //set imagesize
				JLabel label=new JLabel(thumbnail);
				add(label);		
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				//Picturegrid.class.getResource(piclist.get(i));
    	      //	for local pics
			   //ImageIcon Icon=new ImageIcon(getClass().getResource("/cow.JPG")); 
			
		}
			
	}
	
	
	
	private Image getScaledImage(Image srcImg, int w, int h){
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }
    
//	public static void main(String[] args) {
//		final JFrame frame = new JFrame();
//		
//        myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
//        myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
//        myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
//        
//        myArr1.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
//        myArr1.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
//        myArr1.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
//        
//        
//        
//		final Picturegrid pg=new Picturegrid(myArr);
//		final Picturegrid pg2=new Picturegrid(myArr1);
//		
//		final JPanel panel=new JPanel();
//		panel.add(pg);
//		panel.addMouseListener(new MouseAdapter(){
//		public  void mouseClicked(MouseEvent e) {
////			JLabel showWorld = new JLabel("test");
////		    panel.add(showWorld);
////		    panel.setVisible(true);
//			panel.remove(pg);
//		    panel.add(pg2);
//			panel.validate();
//		    System.out.println("Click!");
//
//		}});
//		
//		
//		frame.add(panel);
//		frame.setSize(300,300);
//		frame.setVisible(true);
//	}



	
	
}
